import {
  getToken,
  setToken,
  removeToken,
  setUserName,
  removeUserName,
  removeOutletId,
  setHomeRouter,
  setAuthInfo,
  removeAuthInfo
} from '@/utils/validate.js'
import { loginAjax } from 'Ajax'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  name: '测试人员',
  outletName: '门店管理员',
  avatar:
    'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80',
  roles: [],
  permissions: [],
  infoModify: false,
  homeRouter: {}
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AUTH_INFO: (state, authInfo) => {
    state.authInfo = authInfo
  },
  SET_OUTLET_NAME: (state, outletName) => {
    state.outletName = outletName
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  INFO_MODIFY_VISIBLE: (state, infoModify) => {
    state.infoModify = infoModify
  },
  SET_HOME_ROUTER: (state, homeRouter) => {
    state.homeRouter = homeRouter
  }
}

const actions = {
  // 登录
  Login({ commit }, userInfo) {
    const params = {
      username: userInfo.username.trim(),
      password: userInfo.password
    }
    return new Promise((resolve, reject) => {
      loginAjax
        .login(params)
        .then(res => {
          const obj = {
            path: '/index',
            name: '首页'
          }
          commit('SET_HOME_ROUTER', obj)
          console.log('login', res)
          setToken(res.token)
          setHomeRouter(obj)
          setAuthInfo(res)
          commit('SET_TOKEN', res.token)
          commit('SET_NAME', res.username)
          commit('SET_AUTH_INFO', res)
          setUserName(res.username)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 获取用户信息
  GetInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      // getInfo(state.token)
      //   .then(res => {
      //     const user = res.user
      //     const avatar =
      //       user.avatar == ''
      //         ? require('@/assets/image/profile.jpg')
      //         : process.env.VUE_APP_BASE_API + user.avatar
      //     if (res.roles && res.roles.length > 0) {
      //       // 验证返回的roles是否是一个非空数组
      //       commit('SET_ROLES', res.roles)
      //       commit('SET_PERMISSIONS', res.permissions)
      //     } else {
      //       commit('SET_ROLES', ['ROLE_DEFAULT'])
      //     }
      //     commit('SET_NAME', user.userName)
      //     commit('SET_AVATAR', avatar)
      //     resolve(res)
      //   })
      //   .catch(error => {
      //     reject(error)
      //   })
    })
  },

  // 退出系统
  LogOut({ commit }) {
    return new Promise((resolve, reject) => {
      loginAjax
        .logout()
        .then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          removeUserName()
          removeAuthInfo()
          this.dispatch('permission/setRoutes', [])
          this.dispatch('tagsView/delAllViews', [])
          resetRouter()
          removeOutletId()
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // 前端 登出
  FedLogOut({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      removeUserName()
      removeAuthInfo()
      resolve()
    })
  },
  InfoModify({ commit }, bool) {
    commit('INFO_MODIFY_VISIBLE', bool)
  },
  SetUserName({ commit }, name) {
    commit('SET_NAME', name)
  },
  SetOutletName({ commit }, name) {
    commit('SET_OUTLET_NAME', name)
  },
  SetHomeRouter({ commit }, obj) {
    commit('SET_HOME_ROUTER', obj)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
